var vmap = {WKID:4326,isPano:false};
require([
  "esri/map","esri/graphic","esri/geometry/Point","esri/SpatialReference","esri/tasks/GeometryService","esri/geometry/Polygon",
  "esri/tasks/LengthsParameters","esri/tasks/AreasAndLengthsParameters","esri/tasks/FindTask","esri/tasks/FindParameters",
  "esri/tasks/query","esri/tasks/QueryTask","esri/dijit/Print",
  "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer","esri/layers/FeatureLayer", 
  "esri/tasks/ClassBreaksDefinition", "esri/tasks/AlgorithmicColorRamp","esri/geometry/screenUtils",
  "esri/tasks/GenerateRendererParameters", "esri/tasks/GenerateRendererTask","esri/dijit/OverviewMap","esri/dijit/BasemapGallery","esri/dijit/Scalebar",
  "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", 
  "esri/toolbars/draw","esri/toolbars/navigation",
  "esri/dijit/PopupTemplate", "esri/dijit/Legend", "esri/config",
  "dojo/parser", "dojo/_base/array", "dojo/_base/lang","dojox/encoding/base64","esri/Color","dojo/on","dojo/dom-attr","dojo/dom-class","dojo/dom-style","dojo/query",
  "dojo/dom", "dojo/dom-construct","dijit/registry","dojo/json",
  "dojo/domReady!"
], function(
  Map, Graphic,Point,SpatialReference,GeometryService,Polygon,
  LengthsParameters,AreasAndLengthsParameters,FindTask,FindParameters,
  Query, QueryTask,Print,
  ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer,FeatureLayer, 
  ClassBreaksDefinition, AlgorithmicColorRamp,screenUtils,
  GenerateRendererParameters, GenerateRendererTask,OverviewMap,BasemapGallery,Scalebar,
  SimpleLineSymbol, SimpleFillSymbol, 
  Draw,Navigation,
  PopupTemplate, Legend,esriConfig,
  parser, arrayUtils,lang,base64,Color,on,
  domAttr,domClass,domStyle,query,dom, domConstruct,registry,JSON
) {
     truevision= new TrueVision("flashContent", appConfig.contPath+"/scripts/flash/TrueMapExplorer.swf", "TME");
	 parser.parse();
	 var map;
	 esriConfig.defaults.io.proxyUrl = appConfig.contPath+"/gis/proxy.jsp";
     esriConfig.defaults.io.alwaysUseProxy = false;
 	var highlightSymbol=new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([78, 171, 214 ]), 4);
	var highlightFillSymbol= new esri.symbol.SimpleFillSymbol("solid", null,  new Color([255, 0, 255, 0.50]));
	var carSymbol=esri.symbol.PictureMarkerSymbol(appConfig.contPath+"/images/gis/streetview-ani.gif", 25, 29);
	var basemap = new ArcGISTiledMapServiceLayer(appConfig.gisPath+"/arcgis/rest/services/fsjt/bj/MapServer"); //底图
	var sdRef = new ArcGISDynamicMapServiceLayer(appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer",{ "id": "facility" });  // 设施汇编
	var diseaseSymbol=esri.symbol.PictureMarkerSymbol(appConfig.contPath +"/images/tools/arrow-green.png", 52, 52); //标识
	var map,handlerMap;
/**-------------------地图基础定义---------------------------*/
vmap.map = map = new Map("mapContainer", { 
    zoom: 17,
    center:	new Point(113.041529 ,23.048709,new SpatialReference({ wkid: vmap.WKID })), //中心点 113.041529 纬度: 23.048709
    basemap: "osm",
    logo:false,
    nav:false,
    slider: true
  });
map.addLayers([sdRef]);
var searchText = dom.byId("searchText").value;
var method = dom.byId("method").value;
if(method =="road") {
	findRoad(searchText);
}
else if(method =="tunnel") {
	findTunnel(searchText);
} else if(method =="bridgeRoad") {
	findBridgeRoad(searchText);
}
else if(method =="bridgeFoot") {
	findBridgeFoot(searchText);
}else if(method =="bridgeCity") {
	findBridgeCity(searchText);
}
else if(method =="geoLocation") {
	var geoLocation = dom.byId("geoLocation").value;
	centerAtGeoLocation(geoLocation);
}else if(method =="geopoint") {
	var geoLocation = dom.byId("geoLocation").value;
	centerAtGeopoint(geoLocation);
}
else if(method =="xy") {
	var x = dom.byId("x").value;
	var y = dom.byId("y").value;
	centerAtxy(x,y);
}

function centerAtxy (x,y) {
	var pt = new Point(x,y,new SpatialReference(4326));
	var graphic = new esri.Graphic(pt,new esri.symbol.PictureMarkerSymbol(appConfig.contPath +"/images/gis/marker.png",21,25));
	//map.graphics.add(graphic);
	vmap.map.centerAt(pt);
}

function centerAtGeoLocation (geoLocation) {
	console.debug(geoLocation);
	var polygon =  new Polygon(geoLocation);
	console.debug(polygon);
	var graphic = new esri.Graphic(polygon,highlightFillSymbol);
	console.debug(polygon.getCentroid());
	//vmap.map.graphics.add(graphic);
	//vmap.map.centerAt(polygon.getCentroid());
}

function centerAtGeopoint (geoLocation) {
	var pt =  new Point(geoLocation);
	var graphic = new esri.Graphic(pt,new esri.symbol.PictureMarkerSymbol(appConfig.contPath +"/images/gis/marker.png",21,25));
	//vmap.map.centerAt(pt);
}
 function findRoad(searchText) {
	var searchurl = appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer";  
	var findParams = new FindParameters(); 
      findParams.returnGeometry = true; 
      findParams.layerIds = [51,52,53]; 
      findParams.searchFields = ["道路名称","道路编号"];
      findParams.searchText = searchText; 
      var findTask = new esri.tasks.FindTask(searchurl);
      findTask.execute(findParams, function(findResults){
      	if(findResults.length==0){
      		return;
      	}
          map.graphics.clear();
          map.infoWindow.hide();
          var extent;
          var showGraphic;
      	var data = arrayUtils.map(findResults, function(findResult,index) {
      		var graphic = findResult.feature;
  				setGraphicSymbol(findResult.feature);
  				summaryInfo(graphic);
  				map.graphics.add(graphic);
  				extent =getGeometryExtent(graphic.geometry,extent);
  				showGraphic = graphic;
            });
      	var deferred = map.setExtent(extent,true);
		deferred.then(function(){
//			var centrPoint =map.extent.getCenter();
//			var screenPoint = map.toScreen(centrPoint);
//			map.infoWindow.setTitle(showGraphic.getTitle());
//	        map.infoWindow.setContent(showGraphic.getContent());
//	        map.infoWindow.show(centrPoint,map.getInfoWindowAnchor(screenPoint));
		  });
      }); 
	
}
 
 function findTunnel(searchText) {
		var searchurl = appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer";  
		var findParams = new FindParameters(); 
	      findParams.returnGeometry = true; 
	      findParams.layerIds = [32]; 
	      findParams.searchFields = ["隧道名称","隧道代码"];
	      findParams.searchText = searchText; 
	      var findTask = new esri.tasks.FindTask(searchurl);
	      findTask.execute(findParams, function(findResults){
	      	if(findResults.length==0){
	      		return;
	      	}
	          map.graphics.clear();
	          map.infoWindow.hide();
	          var extent;
	          var showGraphic;
	      	var data = arrayUtils.map(findResults, function(findResult,index) {
	      		var graphic = findResult.feature;
	  				setGraphicSymbol(findResult.feature);
	  				summaryInfo(graphic);
	  				map.graphics.add(graphic);
	  				extent =getGeometryExtent(graphic.geometry,extent);
	  				showGraphic = graphic;
	            });
	      	var deferred = map.setExtent(extent,true);
			deferred.then(function(){
//				var centrPoint =map.extent.getCenter();
//				var screenPoint = map.toScreen(centrPoint);
//				map.infoWindow.setTitle(showGraphic.getTitle());
//		        map.infoWindow.setContent(showGraphic.getContent());
//		        map.infoWindow.show(centrPoint,map.getInfoWindowAnchor(screenPoint));
			  });
	      }); 
		
	}
 
 function findBridgeRoad(searchText) {
		var searchurl = appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer";  
		var findParams = new FindParameters(); 
	      findParams.returnGeometry = true; 
	      findParams.layerIds = [34]; 
	      findParams.searchFields = ["公路桥编码","公路编码","公路桥名称"];
	      findParams.searchText = searchText; 
	      var findTask = new esri.tasks.FindTask(searchurl);
	      findTask.execute(findParams, function(findResults){
	      	if(findResults.length==0){
	      		return;
	      	}
	          map.graphics.clear();
	          var centerpoint;
	      	var data = arrayUtils.map(findResults, function(findResult,index) {
	      		var graphic = findResult.feature;
	  				setGraphicSymbol(findResult.feature);
	  				summaryInfo(graphic);
	  				map.graphics.add(graphic);
	  				centerpoint =getGeometryCenter(graphic.geometry);
	            });
	          map.centerAndZoom(centerpoint,17);

	      }); 
		
	}
 function findBridgeFoot(searchText) {
		var searchurl = appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer";  
		var findParams = new FindParameters(); 
	      findParams.returnGeometry = true; 
	      findParams.layerIds = [30]; 
	      findParams.searchFields = ["编码"];
	      findParams.searchText = searchText; 
	      var findTask = new esri.tasks.FindTask(searchurl);
	      findTask.execute(findParams, function(findResults){
	      	if(findResults.length==0){
	      		return;
	      	}
	          map.graphics.clear();
	          map.infoWindow.hide();
	          var centerpoint;
	          var showGraphic;
	      	var data = arrayUtils.map(findResults, function(findResult,index) {
	      		var graphic = findResult.feature;
	  				setGraphicSymbol(findResult.feature);
	  				summaryInfo(graphic);
	  				map.graphics.add(graphic);
	  				centerpoint =getGeometryCenter(graphic.geometry);
	  				showGraphic = graphic;
	            });
	      	var deferred =map.centerAndZoom(centerpoint,8);
			deferred.then(function(){
//				var centrPoint =map.extent.getCenter();
//				var screenPoint = map.toScreen(centrPoint);
//				map.infoWindow.setTitle(showGraphic.getTitle());
//		        map.infoWindow.setContent(showGraphic.getContent());
//		        map.infoWindow.show(centrPoint,map.getInfoWindowAnchor(screenPoint));
			  });
	      }); 
	}
 
 function findBridgeCity(searchText) {
		var searchurl = appConfig.gisPath+"/arcgis/rest/services/fsjt/ss/MapServer";  
		var findParams = new FindParameters(); 
	      findParams.returnGeometry = true; 
	      findParams.layerIds = [33]; 
	      findParams.searchFields = ["桥梁编码"];
	      findParams.searchText = searchText; 
	      var findTask = new esri.tasks.FindTask(searchurl);
	      findTask.execute(findParams, function(findResults){
	      	if(findResults.length==0){
	      		return;
	      	}
	          map.graphics.clear();
	          map.infoWindow.hide();
	          var extent;
	          var showGraphic;
	      	var data = arrayUtils.map(findResults, function(findResult,index) {
	      		var graphic = findResult.feature;
	  				setGraphicSymbol(findResult.feature);
	  				summaryInfo(graphic);
	  				map.graphics.add(graphic);
	  				extent =getGeometryExtent(graphic.geometry,extent);
	  				showGraphic = graphic;
	            });
	      	var deferred = map.setExtent(extent,true);
			deferred.then(function(){
//				var centrPoint =map.extent.getCenter();
//				var screenPoint = map.toScreen(centrPoint);
//				map.infoWindow.setTitle(showGraphic.getTitle());
//		        map.infoWindow.setContent(showGraphic.getContent());
//		        map.infoWindow.show(centrPoint,map.getInfoWindowAnchor(screenPoint));
			  });
	      }); 
		
	}

//设置高亮的模式
 function setGraphicSymbol(graphic) {
 	var geometry  = graphic.geometry;
 	  switch(geometry.type){
 	    case "point":
 	    	 graphic.setSymbol(diseaseSymbol);
 	      break;
 	    case "polyline":
 	    	 graphic.setSymbol(highlightSymbol);
 	      break;
 	    case "polygon":
 	    	graphic.setSymbol(highlightFillSymbol);
 	      break;
 	  }  
 }
 
//设置高亮的模式
 function getMapExtent(graphic) {}
 
//---------------------设施信息查询-----------------------------//
//绑定概要信息
function summaryInfo(graphic){
	var point  = getGeometryCenter(graphic.geometry);
	var roadName =  graphic.attributes["桥梁名称"]||graphic.attributes["隧道名称"]||graphic.attributes["道路名称"]||graphic.attributes["所在道路名"]||graphic.attributes["所在道路"]||"未命名"
	infoTemplate = new esri.InfoTemplate("概要信息", (roadName)
			+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a title='实景影像' onclick='vmap.detailsTrueMap()' style='cursor:hand;color:red'>实景影像</a>");
	graphic.setInfoTemplate(infoTemplate);
}

vmap.detailsTrueMap = function detailsTrueMap () {
    var feature = vmap.map.infoWindow.getSelectedFeature();  
    var  point ;
    if(!feature) {
    	point = vmap.map.extent.getCenter();
    } else {
    	 point = getGeometryCenter(feature.geometry);
    }
    vmap.trueMapInf(point.x,point.y);
}
function getGeometryCenter (geometry) {
	 var point;
	  switch(geometry.type){
	    case "point":
	    	point = geometry;
	      break;
	    case "polyline":
	    	point = geometry.getExtent().getCenter();
	      break;
	    case "polygon":
	    	point = geometry.getExtent().getCenter();
	      break;
	  }
	  return point;
}

/*
 *开启实景影像
**/
function showPanoInfo(ev){ 
	
	  // map.centerAt(ev.mapPoint);
	   vmap.trueMapInf(ev.mapPoint.x,ev.mapPoint.y);
}
//显示实景
vmap.trueMapInf =function trueMapInf(x ,y) {
	 var dil = registry.byId("trueMapD");
	  dil.show();
	  showVisionByLngLat(x,y);
	 // vmap.detailTotalRoadInfo();
};

function addPanoAction () {
	 
	}

//对地图进行初始化
function initFunctionality(map) {
	//vmap.navToolbar = new Navigation(vmap.map);
	//vmap.tb = new esri.toolbars.Draw(vmap.map);
	//on(vmap.tb, "draw-end", addGraphics);
	addPanoAction();
};
on(map, "load", initFunctionality);


function getGeometryExtent (geometry,extent) {
	 var extentUnion;
	  switch(geometry.type){
	    case "point":
	    	//extentUnion =geometry;
	      break;
	    default: 
	    	extentUnion =geometry.getExtent();
	  }
	  if(extent){
		  extent.union(extentUnion);
	  }else {
		  extent =extentUnion;
	  }
	  return extent;
}

});